package com.github.jdk8Stream.basic;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Created by zhangcheng03 on 2021-1-25 18:59.
 */
public class Main {

    public static void main(String[] args) {
//        learnStream();
//        reduce3th();
//        reduce4th();
//        reduce5th();
        learnMap();
    }


    private static void learnStream() {
        List<Integer> lists = new ArrayList<>();
        lists.add(1);
        lists.add(2);
        lists.add(3);
        lists.add(4);
        lists.add(5);
        lists.add(6);

        Optional<Integer> sum = lists.stream().reduce((a, b) -> a + b);
        if (sum.isPresent()) System.out.println("list的总和为:" + sum.get());//21
        //<====> lists.stream().reduce((a, b) -> a + b).ifPresent(System.out::println);

        Integer sum2 = lists.stream().reduce(0, (a, b) -> a + b);//21
        System.out.println("list的总和为:" + sum2);

        Optional<Integer> product = lists.stream().reduce((a, b) -> a * b);
        if (product.isPresent()) System.out.println("list的积为:" + product.get());//720

        Integer product2 = lists.stream().reduce(1, (a, b) -> a * b);
        System.out.println("list的积为:" + product2);//720


        Integer product3 = lists.stream().reduce(1, (a, b) -> {
            if (b % 2 == 0) return a * b; else return a;//这里你可以为所欲为!
        });
        System.out.println("list的偶数的积为:" + product3);//48
    }


    private static void reduce3th() {
        List<Integer> lists = new ArrayList<>();
        lists.add(1);
        lists.add(2);
        lists.add(3);
        lists.add(4);
        lists.add(5);
        lists.add(6);

        Integer product2 = lists.parallelStream().reduce(1, (a, b) -> a * b,
                (a, b) -> a * b);
        System.out.println("list的积为:" + product2);//720
    }

    private static void reduce4th() {
        List<Integer> lists = new ArrayList<>();
        lists.add(1);
        lists.add(2);
        lists.add(3);

        Integer product = lists.parallelStream().reduce(1, (a, b) -> a *  (b * 2),
                (a, b) -> a * b);
        System.out.println("product:" + product);//48
    }

    private static void reduce5th() {
        List<Integer> lists = new ArrayList<>();
        lists.add(1);
        lists.add(2);
        lists.add(3);

        Integer product = lists.parallelStream().reduce(1, (a, b) -> a *  (b * 2),
                (a, b) -> a * b * 2 );
        System.out.println("product:" + product);//192
    }

    private static void learnMap() {
        List<Integer> lists = new ArrayList<>();
        lists.add(1);
        lists.add(2);
        lists.add(3);
        //使用并行流来处理
        Integer product = lists.parallelStream().reduce(1, (a, b) -> a *  (b * 2),
                (a, b) -> a * b);
        System.out.println("product:" + product);//48

        //使用映射来处理
        //Integer productMap = lists.parallelStream().map((a) -> a * 2).reduce(1, (a, b) -> a * b);
        Stream<Integer> productNewMapStream = lists.parallelStream().map((a) -> a * 2);
        Integer productMap = productNewMapStream.reduce(1, (a, b) -> a * b);
        System.out.println("productMap:" + productMap);//48
    }
}